package leetcode.d27;

//nums = [1,1,1,1,1], target = 3
public class Solution {

    public static void main(String[] args) {
        int[] nums = {0,0,0,0,0,0,0,0,1};
        int[] b = {1,1,1,1,1};
        int[] c = {1,2,1};
        int[] d = {9,7,0,3,9,8,6,5,7,6};
        int targetSumWays = findTargetSumWays(nums, 1);
        System.out.println(targetSumWays);
    }
    //TODO
    public static int findTargetSumWays(int[] nums, int target) {
        if (nums.length == 1){
            if (nums[0] == target || 0-nums[0] == target){
                return 1;
            }
        }
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        if ((sum + target) % 2 != 0) {
            return 0;
        }
        int value = (sum + target) / 2;
        int[] d = new int[value + 1];
        d[0] = 1;
        //初始化
        for (int i = 0; i < nums.length; i++) {
            for (int j = value; j >= nums[i]; j--) {
                d[j] +=d[j-nums[i]];
            }
        }
        return d[value];
    }
}
